package info.scholtus.open.ui;

import info.scholtus.open.R;
import info.scholtus.open.provider.Contract.Categories;
import info.scholtus.open.ui.HomeActivity.DummySectionFragment;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class VendorsListActivity extends SherlockFragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

	private static final int LOADER_CATEGORY = 0;
	private static final int LOADER_ALL = 1;
	private static final int LOADER_STARRED = 2;
	
	/** The {@link Uri} representing this Activity scope. */
	private Uri mUri;
	
	/**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    VendorsPagerAdapter mSectionsPagerAdapter;

    /** The {@link ViewPager} that will host the section contents. */
    ViewPager mViewPager;

	@Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mUri = getIntent().getData(); 
		
        setContentView(R.layout.activity_home);
        
        // Create the adapter that will return a fragment 
        // for each of the three primary sections of the app.
        mSectionsPagerAdapter = new VendorsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
	
	@Override
	protected void onResume() {
		super.onResume();

        getSupportLoaderManager().initLoader(LOADER_CATEGORY, null, this);
	}
    
    @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    	
		final String[] projection = new String[] {
				BaseColumns._ID,
				Categories.CATEGORY_NAME
		};
		
		return new CursorLoader(this, mUri, projection, null, null, null);
	}

    @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    	// TODO Auto-generated method stub
    	data.moveToFirst();
    	getSupportActionBar().setTitle(data.getString(1));
    	setTitle(data.getString(1));
	}

    @Override public void onLoaderReset(Loader<Cursor> arg0) {
    	// TODO Auto-generated method stub
	}
    
	/**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class VendorsPagerAdapter extends FragmentStatePagerAdapter {

        public VendorsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override public Fragment getItem(int i) {
        	
        	Fragment fragment;
        	
        	switch (i) {
        	case 0:
	        	fragment = new DummySectionFragment();
	            Bundle args = new Bundle();
	            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, 1);
	            fragment.setArguments(args);
	            break;
        	case 1:
        	default: fragment = null;
        	}
        	
            return fragment;
        }

        @Override public int getCount() {
            return 1;
        }

        @Override public CharSequence getPageTitle(int position) {
            switch (position) {
            	case 0: return getString(R.string.title_all).toUpperCase();
                case 1: return getString(R.string.title_starred).toUpperCase();
            }
            return null;
        }
    }
}
